# 爬取百度街拍图片，用requests爬取图片，pyquery解析

import requests
from pyquery import PyQuery as pq
import time,os,json,time
from urllib.parse import urlencode
from urllib.request import urlretrieve

def getPage(pn):
    '''爬取信息'''
    # 定义参数
    parmas = {
        'tn':'resultjson_com',
        'ipn':'rj',
        'ct':'201326592',
        'is':'',
        'fp':'result',
        'queryWord':'街拍',
        'lm':'-1',
        'ie':'utf-8',
        'oe':'utf-8',
        'adpicid':'',
        'st':'-1',
        'z':'',
        'ic':'0',
        'word':'街拍',
        'se':'',
        'tab':'',
        'width':'',
        'height':'',
        'face':'0',
        'istype':'2',
        'qc':'',
        'nc':'1',
        'fr':'',
        'pn':pn,
        'rn':'30',
        'gsm':'1e',
        '1530541668936':'',
    }
    # 初始化url地址
    url = 'https://image.baidu.com/search/acjson?'+urlencode(parmas)
    try:
        res = requests.get(url) # 爬取网页内容并转码
        if res.status_code == 200:
            return res.json()
    except:
        return None
def getImage(json):
    '''获取图片信息'''
    data = json.get("data")
    if data:
        try:
            for item in data:
                middleURL = item.get("middleURL")
                #fromPageTitle = item.get("fromPageTitleEnc")    
                if item:
                    yield {
                        "middleURL":middleURL,
                        #"fromPageTitle":fromPageTitle,
                    }
            return None
        except Exception as e:
            print(e)
        
def saveImage(item):
    
    # 处理图片储存的路径
    
    path = os.path.join("./mypic/",'%s'%time.time())
    if not os.path.exists(path):
        os.mkdir(path)

    #图片路径的处理
    local_image_url = item.get("middleURL")
    save_pic =path+"/"+'%s'%time.time()+".jpg"

    #存储图片
    urlretrieve(local_image_url,save_pic)

    

def main(pn):
    '''调度函数'''
    json = getPage(pn)
    for item in getImage(json):
        print('当前下载第%d页图片'%(i+1))
        saveImage(item)


if __name__ == '__main__':
    for i in range(5):
        main(pn=i*30)
        time.sleep(1)


